<?php

namespace plugin\ycMassage\app\api\controller;

use app\common\service\UploadService;
use plugin\ycMassage\app\BaseController;
use plugin\ycMassage\app\model\MassageService;
use plugin\ycMassage\app\model\MassageUserTechnician;
use plugin\ycMassage\app\model\MassageUserTechnicianService;
use plugin\ycMassage\utils\enum\Examine;
use plugin\ycMassage\utils\enum\State;
use support\Request;

class TechnicianServiceController extends BaseController
{
    public function selection(Request $request)
    {
        $limit = $request->get('limit', 10);
        $page = $request->get('page', 1);
        $where = [];
        $where[] = ['saas_appid', '=', $request->saas_appid];
        $where[] = ['state', '=', State::YES['value']];
        $where[] = ['examine', '=', Examine::YES['value']];
        $keywords = $request->get('keywords', '');
        if ($keywords) {
            $where[] = ['title', 'like', "%{$keywords}%"];
        }
        $model = MassageService::where($where);
        $MassageUserTechnician = MassageUserTechnician::where('uid', $request->uid)->find();
        if ($MassageUserTechnician->work_mch_id) {
            $model->where(function ($query) use ($MassageUserTechnician) {
                $query->whereOr([
                    ['mch_id', '=', $MassageUserTechnician->work_mch_id],
                    ['mch_id', '=', NULL]
                ]);
            });
            if ($MassageUserTechnician->work_shop_id) {
                $model->where(function ($query) use ($MassageUserTechnician) {
                    $query->whereOr([
                        ['shop_id', '=', $MassageUserTechnician->work_shop_id],
                        ['shop_id', '=', NULL]
                    ]);
                });
            }
        } else {
            $model->whereNull('mch_id');
            $model->whereNull('shop_id');
        }
        $notInIds = MassageUserTechnicianService::where('uid', $request->uid)->column('service_id');
        if ($notInIds) {
            $model->whereNotIn('id', $notInIds);
        }
        $data = $model->paginate([
            'list_rows' => $limit,
            'page' => $page
        ])->each(function ($item) {
            $item->is_join = false;
        });
        return $this->successRes($data);
    }
    public function joinService(Request $request)
    {
        $service_id = $request->post('service_id');
        if ($service_id) {
            $service = MassageService::where('id', $service_id)->find();
            if ($service) {
                if ($service->mch_id || $service->shop_id) {
                    $MassageUserTechnician = MassageUserTechnician::where('uid', $request->uid)->find();
                    if ($MassageUserTechnician->work_mch_id != $service->mch_id) {
                        return $this->fail('您无法加入该服务');
                    }
                    if ($service->shop_id && $MassageUserTechnician->work_shop_id != $service->shop_id) {
                        return $this->fail('您无法加入该服务');
                    }
                }
                $MassageUserTechnicianService = new MassageUserTechnicianService;
                if ($MassageUserTechnicianService->where(['service_id' => $service_id, 'uid' => $request->uid])->find()) {
                    return $this->fail('您已加入该服务');
                } else {
                    $MassageUserTechnicianService->uid = $request->uid;
                    $MassageUserTechnicianService->service_id = $service_id;
                    $MassageUserTechnicianService->saas_appid = $request->saas_appid;
                    $MassageUserTechnicianService->state = State::YES['value'];
                    if ($MassageUserTechnicianService->save()) {
                        return $this->success('加入成功');
                    } else {
                        return $this->fail('加入失败');
                    }
                }
            }
        }
        return $this->fail('服务不存在');
    }
    public function service(Request $request)
    {
        $limit = $request->get('limit', 10);
        $page = $request->get('page', 1);
        $where = [];
        $where[] = ['uts.saas_appid', '=', $request->saas_appid];
        $where[] = ['uts.uid', '=', $request->uid];
        $keywords = $request->get('keywords', '');
        if ($keywords) {
            $where[] = ['title', 'like', "%{$keywords}%"];
        }
        $model = MassageUserTechnicianService::alias('uts')->where($where);
        $data = $model->join('massage_service ms', 'ms.id = uts.service_id')
            ->field('ms.*,uts.id as uts_id,uts.state')
            ->paginate([
                'list_rows' => $limit,
                'page' => $page
            ])->each(function ($item) {
                $item->thumb = UploadService::url($item->thumb);
            });
        return $this->successRes($data);
    }
    public function updateState(Request $request)
    {
        $uts_id = $request->post('uts_id');
        if ($uts_id) {
            $MassageUserTechnicianService = MassageUserTechnicianService::where('id', $uts_id)->find();
            if ($MassageUserTechnicianService) {
                $MassageUserTechnicianService->state = $MassageUserTechnicianService->state == State::YES['value'] ? State::NO['value'] : State::YES['value'];
                if ($MassageUserTechnicianService->save()) {
                    return $this->successRes($MassageUserTechnicianService);
                } else {
                    return $this->fail('更改失败');
                }
            }
        }
        return $this->fail('服务不存在');
    }
}
